package cn.itcast.up.up_web.controller;

import cn.itcast.up.common.HDFSUtils;
import cn.itcast.up.up_web.bean.Codes;
import cn.itcast.up.up_web.bean.HttpResult;
import cn.itcast.up.up_web.bean.dto.ModelDto;
import cn.itcast.up.up_web.bean.dto.TagDto;
import cn.itcast.up.up_web.bean.dto.TagModel;
import cn.itcast.up.up_web.service.TagService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
public class TagController {
    @Autowired
    private TagService tagService;

    @PutMapping("tags/relation")
    public void addTags(@RequestBody List<TagDto> list) {
        tagService.saveAll(list);
    }

    @GetMapping("tags")
    public HttpResult<List<TagDto>> findTags(Long pid) {
        List<TagDto> list = tagService.findTagsByPid(pid);
        return new HttpResult<List<TagDto>>(Codes.SUCCESS, "成功", list);
    }

    @PutMapping("tags/model")
    public void saveTagModel(@RequestBody TagModel tagModel) {
        tagService.saveTagModel(tagModel.getTag(), tagModel.getModel());
    }

    @GetMapping("tags/model")
    public HttpResult getTagModel(Long pid) {
        List<TagModel> list = null;
        list = tagService.findAllTagModel(pid);
        HttpResult<List<TagModel>> result = new HttpResult<>(Codes.SUCCESS, "成功", list);
        return result;
    }

    //    请求网址:http://localhost:8081/tags/data
//    请求方法:PUT
//{"name":"asd","business":"adf","industry":"","rule":"dg","level":5,"pid":"604"}
    @PutMapping("tags/data")
    public HttpResult putFiveTag(@RequestBody TagDto tagDto) {
        tagService.saveTag(tagDto);
        return new HttpResult(Codes.SUCCESS, "成功", tagDto);
    }

    //    请求网址:http://localhost:8081/tags/604/model
//    请求方法:POST
//{"state":3}
    @PostMapping("tags/{id}/model")
    public HttpResult changeModelState(@PathVariable Long id, @RequestBody ModelDto modelDto) {
        tagService.updateModelState(id, modelDto.getState());
        return new HttpResult(Codes.SUCCESS, "成功", null);
    }
    //    请求网址:http://localhost:8081/tags/upload
//    请求方法:POST
//    Content-Disposition: form-data; name="file"; filename="avalon-framework-api-4.3.jar"
//    Content-Type: application/octet-stream
//    参数名字必须叫file，与上面的name对应
    @PostMapping("tags/upload")
    public HttpResult uploadFile(MultipartFile file) throws IOException {
//      1.  接收文件
        String type = file.getContentType();
        String name = file.getName();
//      上传文件的名字
        String originalFilename = file.getOriginalFilename();
        long size = file.getSize();
        InputStream inputStream = file.getInputStream();
//        2.将文件缓存本地
        IOUtils.copy(inputStream, new FileOutputStream(new File("a.jar")));
//       3. 将本地上传到hdfs
        String fileName = UUID.randomUUID().toString() + ".jar";
        String path = "/tmp/jars/" + fileName;
//       4. 将hdfs的路径返回给前端
        HDFSUtils.getInstance().copyFromFile("a.jar", path);
        HttpResult<String> success = new HttpResult<>(Codes.SUCCESS, "success", "hdfs://bd001:8020" + path);
        return success;
    }
}
